/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.fpt.xml.hrm.controller.auth;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.sql.DataSource;

/**
 *
 * @author AnhNH60400
 */
public class LoginObjJDBC implements Serializable {

    private String username;
    private String password;
    private String displayname;
    private String employeeid;

    public String getEmployeeid() {
        return employeeid;
    }

    public void setEmployeeid(String employeeid) {
        this.employeeid = employeeid;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    public String getDisplayname() {
        return displayname;
    }

    public void setDisplayname(String displayname) {
        this.displayname = displayname;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    public LoginObjJDBC() {
    }

    public LoginObjJDBC(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public boolean checkLogin() {
        try {
            Context ctx = new InitialContext();
            Context envCtx = (Context) ctx.lookup("java:comp/env");
            DataSource ds = (DataSource) envCtx.lookup("DBCon");
            Connection con = ds.getConnection();
            System.out.println("Connection String: " + con.toString());
            String sql = "SELECT e.EmployeeID, u.DisplayName "
                    + "FROM Employee e INNER JOIN \"User\" u ON e.UserID = u.UserID "
                    + "WHERE u.UserName = ? and u.Password = ? and u.Status = 'Approved'";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, username);
            stm.setString(2, password);
            ResultSet rs = stm.executeQuery();
            boolean result = rs.next();
            displayname= rs.getString("DisplayName");
            employeeid = rs.getString("EmployeeID");
            rs.close();
            stm.close();
            con.close();
            if (result) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public boolean isAdmin() {
        boolean Manager = false;
        PreparedStatement sta = null;
        ResultSet rs = null;
        try {
            Context ctx = new InitialContext();
            Context envCtx = (Context) ctx.lookup("java:comp/env");
            DataSource ds = (DataSource) envCtx.lookup("DBCon");
            Connection con = ds.getConnection();
            String sql = "Select * from \"User\" where UserName = ? and Password = ?";
            sta = con.prepareStatement(sql);
            sta.setString(1, username);
            sta.setString(2, password);
            rs = sta.executeQuery();
            if (rs.next()) {
                Manager = rs.getBoolean("isAdmin");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (sta != null) {
                    sta.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return Manager;
    }

    public String checkStatus() {
        String Status = "";
        PreparedStatement sta = null;
        ResultSet rs = null;
        try {
            Context ctx = new InitialContext();
            Context envCtx = (Context) ctx.lookup("java:comp/env");
            DataSource ds = (DataSource) envCtx.lookup("DBCon");
            Connection con = ds.getConnection();
            String sql = "Select * from \"User\" where username = ? and password = ? ";
            sta = con.prepareStatement(sql);
            sta.setString(1, username);
            sta.setString(2, password);
            rs = sta.executeQuery();
            if (rs.next()) {
                Status = rs.getString("Status");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (sta != null) {
                    sta.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return Status;
    }

//    public boolean getEmployeeID() {
//        try {
//            Context ctx = new InitialContext();
//            Context envCtx = (Context) ctx.lookup("java:comp/env");
//            DataSource ds = (DataSource) envCtx.lookup("DBCon");
//            Connection con = ds.getConnection();
//            System.out.println("Connection String: " + con.toString());
//            String sql = "SELECT EmployeeID FROM Employee WHERE (SELECT UserID FROM \"User\" WHERE \"User\".UserName = ?)= Employee.UserID";
//            PreparedStatement stm = con.prepareStatement(sql);
//            stm.setString(1, username);
//            stm.setString(2, password);
//            ResultSet rs = stm.executeQuery();
//            boolean result = rs.next();
//            employeeid= rs.getString("EmployeeID");
//            rs.close();
//            stm.close();
//            con.close();
//            if (result) {
//                return true;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return false;
//    }

    
}
